package com.electromagnetic.industry.software.manage.service.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.electromagnetic.industry.software.common.enums.EffectFlagEnum;
import com.electromagnetic.industry.software.manage.mapper.UserRoleMapper;
import com.electromagnetic.industry.software.manage.pojo.models.UserRole;
import com.electromagnetic.industry.software.manage.service.UserRoleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole> implements UserRoleService {

    /**
     * 逻辑删除用户-角色关联关系
     *
     * @param userId 用户ID（可选）
     * @param roleId 角色ID（可选）
     * @return 操作是否成功
     * @throws IllegalArgumentException 参数校验失败时抛出
     */
    @Transactional
    @Override
    public boolean logicRemove(String userId, String roleId) {
        // 参数校验
        if (userId == null && roleId == null) {
            throw new IllegalArgumentException("必须提供至少一个参数");
        }
        // 执行逻辑删除
        return this.update(
                new LambdaUpdateWrapper<UserRole>()
                        .set(UserRole::getEffectFlag, EffectFlagEnum.NOT_EFFECTIVE.code)
                        .eq(userId != null, UserRole::getUserId, userId)
                        .eq(roleId != null, UserRole::getRoleId, roleId)
                        .eq(UserRole::getEffectFlag, EffectFlagEnum.EFFECT.code)
        );
    }
}

